package com.example.costs;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import java.util.ArrayList;

import base.Lg;

/**
 * Created by Developer on 08.07.13.
 */
public class TrListViewExpAdapter extends BaseExpandableListAdapter {

    private LayoutInflater inflater;
    private ArrayList<ParentLV> mParent;

    public TrListViewExpAdapter(LayoutInflater inflater, ArrayList<ParentLV> mParent) {
        this.inflater = inflater;
        this.mParent = mParent;
    }

    @Override
    public int getGroupCount() {
        return mParent.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return mParent.get(i).getArrayChildren().size();
    }

    @Override
    public Object getGroup(int i) {
        return mParent.get(i).getTitle();
    }

    @Override
    public Object getChild(int i, int i2) {
        return mParent.get(i).getArrayChildren().get(i2);
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i2) {
        return i2;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
        ViewHolder holder = new ViewHolder();
//        holder.groupPosition = groupPosition;
        holder.groupPosition = i;

        if (view == null) {
            view = inflater.inflate(R.layout.list_parent_layout, viewGroup,false);
        }

        TextView textView = (TextView) view.findViewById(R.id.trParentLV);
        //"i" is the position of the parent/group in the list
        textView.setText(getGroup(i).toString());


        view.setTag(holder);

        //return the entire view
        return view;
    }

    @Override
    public View getChildView(int i, int i2, boolean b, View view, ViewGroup viewGroup) {
        ViewHolder holder = new ViewHolder();
        holder.childPosition = i2;
        holder.groupPosition = i;

        if (view == null) {
            view = inflater.inflate(R.layout.list_item_layout, viewGroup,false);
        }

        Log.d("getChildView","i - "+i+"; i2 - "+i2);
        TextView textViewCtg = (TextView) view.findViewById(R.id.lCtgNameTr);
        TextView textViewPrice = (TextView) view.findViewById(R.id.lPrice);
        //"i" is the position of the parent/group in the list and
        //"i1" is the position of the child
        ArrayList arrayListChild = mParent.get(i).getArrayChildren();
        ArrayList<String> arrayListFields = (ArrayList)arrayListChild.get(i2);
        textViewCtg.setText(arrayListFields.get(0));
        textViewPrice.setText(arrayListFields.get(1));

        view.setTag(holder);

        //return the entire view
        return view;
    }

    @Override
    public boolean isChildSelectable(int i, int i2) {
        return true;
    }

    protected class ViewHolder {
        protected int childPosition;
        protected int groupPosition;
//        protected Button button;
    }
}
